<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2021/3/9
 * Time: 11:24
 */
include '../vendor/phpoffice/phpexcel/Classes/PHPExcel.php';
function import_data()
{
    try {
        //获取上传后的文件
        $fileName = '2022.05.23_erp1.xlsx';
        //实例化PHPExcel类
        $PHPReader = new \PHPExcel_Reader_Excel2007('Excel7');//xlsx
//        $PHPReader = new \PHPExcel_Reader_Excel5('Excel5');//xls
        //读取excel文件
        $objPHPExcel = $PHPReader->load($fileName);
        //读取excel文件中的第一个工作表
        $sheet = $objPHPExcel->getSheet(0);
        $allRow = $sheet->getHighestRow();  //取得总行数
        //$allColumn = $sheet->getHighestColumn();  //取得总列数
        //从第二行开始插入，第一行是列名
//        printf($allRow);exit();
        if ($allRow > 2000) {
            $data = [
                'msg' => '至多一次导入2000条数据'
            ];
            exit(json_encode($data));
        }
        $data=[];
        for ($j = 2; $j <= $allRow; $j++) {
            $data[$j]['model'] = $objPHPExcel->getActiveSheet()->getCell("A" . $j)->getValue();
        }
        $data=array_values(array_column($data,'model'));
        print_r(implode(',',array_map('change_to_quotes', $data )));//获取所有版号
    } catch (Exception $e) {
        $msg = $e->getMessage();
        $data = [
            'msg' => $msg
        ];
        echo json_encode($data);
    }
}

function change_to_quotes($str) {
    return sprintf("'%s'", $str);
}

import_data();
